#!/bin/bash

# Docker构建监控脚本

monitor_container() {
    local container_name=$1
    local refresh_rate=2  # 刷新频率(秒)
    
    echo "监控容器: $container_name (每 $refresh_rate 秒刷新，按 Ctrl+C 退出)"
    echo "----------------------------------------"
    
    while true; do
        clear
        
        # 显示容器状态
        echo "容器状态:"
        docker ps -a | grep $container_name
        
        # 显示容器资源使用情况
        echo -e "\n资源使用情况:"
        docker stats --no-stream $container_name
        
        # 显示最近日志
        echo -e "\n最近日志:"
        docker logs --tail 10 $container_name 2>&1
        
        sleep $refresh_rate
    done
}

monitor_build() {
    local refresh_rate=2  # 刷新频率(秒)
    
    echo "监控Docker构建进程 (每 $refresh_rate 秒刷新，按 Ctrl+C 退出)"
    echo "----------------------------------------"
    
    while true; do
        clear
        
        # 显示Docker进程
        echo "Docker进程:"
        ps aux | grep -E 'docker (build|compose)' | grep -v grep
        
        # 显示系统资源
        echo -e "\n系统资源使用情况:"
        top -b -n 1 | head -n 10
        
        # 显示磁盘使用情况
        echo -e "\nDocker磁盘使用:"
        docker system df
        
        sleep $refresh_rate
    done
}

# 主菜单
echo "Docker监控工具"
echo "1. 监控容器 (需提供容器名)"
echo "2. 监控构建进程"
echo "请选择操作 [1-2]:"
read choice

case $choice in
    1)
        echo "输入容器名称:"
        read container_name
        monitor_container $container_name
        ;;
    2)
        monitor_build
        ;;
    *)
        echo "无效选择"
        exit 1
        ;;
esac 